package com.meals.system.config.handler;

import com.meals.system.common.Constants;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.common.SystemException;
import com.meals.system.config.annotation.TokenToSystemUser;
import com.meals.system.dao.SystemUserMapper;
import com.meals.system.dao.SystemUserTokenMapper;
import com.meals.system.entity.SystemUser;
import com.meals.system.entity.SystemUserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/21
 */
@Component
public class TokenToUserMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private SystemUserTokenMapper systemUserTokenMapper;

    public TokenToUserMethodArgumentResolver(){

    }
    public boolean supportsParameter(MethodParameter parameter){
        if (parameter.hasParameterAnnotation(TokenToSystemUser.class)){
            return true;
        }
        return false;
    }

    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory webDataBinderFactory){
        if (parameter.getParameterAnnotation(TokenToSystemUser.class)instanceof  TokenToSystemUser){
            SystemUser systemUser = null;
            String token = webRequest.getHeader("token");
            if (null != token && !"".equals(token) && token.length() == Constants.TOKEN_LENGTH){
                SystemUserToken systemUserToken = systemUserTokenMapper.selectByToken(token);
                if (systemUserToken == null || systemUserToken.getExpireTime().getTime() <= System.currentTimeMillis()){
                    SystemException.fail(ServiceResultEnum.TOKEN_EXPIRE_ERROR.getResult());
                }
                systemUser = systemUserMapper.selectByPrimaryKey(systemUserToken.getUserId());
                if (systemUser == null){
                    SystemException.fail(ServiceResultEnum.USER_NULL_ERROR.getResult());
                }
                if (systemUser.getLockedFlag().intValue() == 1){
                    SystemException.fail(ServiceResultEnum.LOGIN_USER_LOCKED_ERROR.getResult());
                }
                return systemUser;
            }
        } else {
            SystemException.fail(ServiceResultEnum.NOT_LOGIN_ERROR.getResult());
        }
        return null;
    }


    public static byte[] getRequestPostBytes(HttpServletRequest request)throws IOException {
        int contentLength = request.getContentLength();
        if (contentLength < 0){
            return null;
        }
        byte buffer[] = new byte[contentLength];
        for (int i = 0; i < contentLength;){
            int readlen = request.getInputStream().read(buffer, i, contentLength - i);
            if (readlen == -1){
                break;
            }
            i += readlen;
        }
        return buffer;
    }

}
